//
//  DynamicDetailViewController.m
//  NOAHWM
//
//  Created by Ryan on 13-6-26.
//  Copyright (c) 2013年 Ryan. All rights reserved.
//

#import "DynamicDetailViewController.h"
#import "NYMemberCenterDataManager.h"
#import "NSString+HtmlCss.h"
#import "JSON.h"

@interface DynamicDetailViewController ()

@end

@implementation DynamicDetailViewController

@synthesize accountDynamic,dynamicDetail;
@synthesize detailWebView;
@synthesize isRightMenuEnter;

#pragma mark - BaseViewController methods
- (void)leftItemTapped
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightItemTapped
{
    NSArray *properties = [NSArray arrayWithObject:@"Call"];
    NSMutableDictionary *filterInfo = [NSMutableDictionary dictionary];
    [filterInfo setObject:properties forKey:kFilterPropertiesKey];
    [filterInfo setObject:[NSNumber numberWithInt:kLocationRight] forKey:kFilterLocationType];
    [filterInfo setObject:[NSNumber numberWithInteger:kDynamicDetailFilter] forKey:kFilterTypeKey];
    [[NSNotificationCenter defaultCenter] postNotificationName:kShowFilterViewNotification object:nil userInfo:filterInfo];
}


#pragma mark - UIViewController methods

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setNaviTitle:self.accountDynamic.fapp_title];
    [self setLeftNaviItemWithTitle:nil imageName:@"Icon_Back"];
    [self setRightNaviItemWithTitle:nil imageName:@"Icon_Dial"];
    
    NSString *suburl = [NSString stringWithFormat:kDynamicDetailUrl,@"jsonp",[NYMemberCenterDataManager sharedManager].loginMember.accountId,self.accountDynamic.article_id,self.accountDynamic.article_ref_id,@""];
    if(self.isRightMenuEnter)
    {
        suburl = [suburl stringByAppendingString:@"app000046"];
    }
    else
    {
        suburl = [suburl stringByAppendingString:@"app000032"];
    }
    NSString *url = [NSString stringWithFormat:@"%@%@",kServerAddress,suburl];
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [[RYDownloaderManager sharedManager] requestDataByGetWithURLString:url delegate:self purpose:nil];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.isViewLoaded && !self.view.window)
    {
        [[RYDownloaderManager sharedManager] cancelDownloaderWithDelegate:self purpose:nil];
        self.view = nil;
        self.detailWebView = nil;
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    if(self.isViewLoaded && !self.view.window)
    {
        [[RYDownloaderManager sharedManager] cancelDownloaderWithDelegate:self purpose:nil];
        self.view = nil;
        self.detailWebView = nil;
    }
}

- (void)dealloc
{
    [[RYDownloaderManager sharedManager] cancelDownloaderWithDelegate:self purpose:nil];
    [detailWebView release];
    [dynamicDetail release];
    [accountDynamic release];
    [super dealloc];
}

#pragma mark - RYDownloaderDelegate methods
- (void)downloader:(RYDownloader*)downloader completeWithNSData:(NSData*)data
{
    NSString *str = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
    str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    //去掉被包的jsonp字串
    if(str.length > 7)
        str = [str substringWithRange:NSMakeRange(6, str.length-7)];
    NSDictionary *dict = [str JSONValue];
    if([[dict objectForKey:@"result"] isEqualToString:@"1"])
    {
        NYDynamicDetail *nyDD = [[NYDynamicDetail alloc] init];
        for(NSString *key in [dict allKeys])
        {
            id value = [dict objectForKey:key];
            @try {
                [nyDD setValue:value forKey:key];
            }
            @catch (NSException *exception) {
                    NSLog(@"试图添加不存在的key: %@到NYDynamicDetail实例中.",key);
            }
        }
        self.dynamicDetail = nyDD;
        [nyDD release];
        
        NSString *htmlString = [NSString adjustHtmlCSSForString:self.dynamicDetail.fapp_content];;
        [self.detailWebView loadHTMLString:htmlString baseURL:nil];
        if ([self.accountDynamic.isread isEqualToString:@"0"]) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:kMyReminderReduceNotification object:self.dynamicDetail];
        }
    }
    else
    {
        [[RYHUDManager sharedManager] showWithMessage:[dict objectForKey:@"message"] customView:nil hideDelay:2.f];
    }
}
- (void)downloader:(RYDownloader*)downloader didFinishWithError:(NSString*)message
{
    [[RYHUDManager sharedManager] showWithMessage:kNetWorkErrorString customView:nil hideDelay:2.f];
}


@end
